/*------------------------------------------------------*/
/*                                                      */
/* Copyright (C)	sky-mobi.com                        */
/* All rights reserved                                  */
/*                                                      */
/* Author   tristan.yang                                */
/*                                                      */
/* History  2011-5-10 created                           */
/*                                                      */
/*------------------------------------------------------*/

#include "chatpdu.h"
#include "cmnetbase.h"

RtResult CChatPduDecoder::DecodePdu(CRtMessageBlock& aData, CChatPduBase*& pdu)
{
	RT_ASSERTE(pdu==NULL);

	RtResult rv = RT_OK;
	WORD nType = 0 ;
	CChatPduBase::PeekType(aData , nType);

	try {
		CRtByteStreamIntel bsStream(aData);
		switch(nType)
		{
		case CHAT_PDU_TYPE_BASE:
			break;
		default:
			RT_WARNING_TRACE("CChatPduDecoder::DecodePdu, unknown pdu type! type="<<nType);
			return RT_ERROR_NOT_IMPLEMENTED;
		}
	} 
	catch (...) {
		RT_WARNING_TRACE("CChatPduDecoder::DecodePdu, catch an exception!");
		rv = RT_ERROR_FAILURE;
	}

	if (RT_SUCCEEDED(rv))
		pdu->AddReference();
	else {
		delete pdu;
		pdu = NULL;
	}
	return rv;
}


CRtString GetCurShortTime()
{
	CRtTimeValue tvCur = CRtTimeValue::GetTimeOfDay();
	time_t sec = tvCur.GetSec();
	
	char szTime[100];
	struct tm tmVar;
#if !defined (RT_WIN32)
	localtime_r(&sec, &tmVar);
#else
	tmVar = *localtime(&sec);
#endif //!defined (RT_WIN32)
	
	sprintf(szTime, "%04d-%02d-%02d %02d:%02d:%02d",
		tmVar.tm_year + 1900,
		tmVar.tm_mon + 1,
		tmVar.tm_mday,
		tmVar.tm_hour,
		tmVar.tm_min,
		tmVar.tm_sec);
	return szTime;
}

struct tm getNow()
{
	CRtTimeValue tvCur = CRtTimeValue::GetTimeOfDay();
	time_t sec = tvCur.GetSec();
	struct tm tmVar;
#if !defined (RT_WIN32)
	localtime_r(&sec, &tmVar);
#else
	tmVar = *localtime(&sec);
#endif //!defined (RT_WIN32)
	return tmVar;
}